package com.example.sunjian.myapplication.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.sunjian.myapplication.R;

import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.LoggingPermission;

/**
 * 作者：sunjian on 16/7/2 13:50
 * 邮箱：sunjian@cyhz.com
 * handler机制
 * 1、在handler被创建时，关联looper，同时创建MessageQueue
 * 2、looper内的loop方法内到MessageQueue内轮询消息，获取到message，调用msg.target.dispatchMessage(msg);
 *  msg.target就是当前处理消息的hander
 *  private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
 msg.target = this;
 if (mAsynchronous) {
 msg.setAsynchronous(true);
 }
 return queue.enqueueMessage(msg, uptimeMillis);
 }

3、 再执行handler内的
 public void dispatchMessage(Message msg) {
 if (msg.callback != null) {
 handleCallback(msg);
 } else {
 if (mCallback != null) {
 if (mCallback.handleMessage(msg)) {
 return;
 }
 }
 handleMessage(msg);
 }
 }
 *
 */
public class HandlerDemoActivity extends Activity implements Handler.Callback{


    private Handler mHandler;
    private Button mBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handler_demo);

        mBtn = (Button) findViewById(R.id.send_message);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        mHandler.sendEmptyMessage(110);
                    }

                }.start();
            }
        });



        findViewById(R.id.send_message1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        mHandler.sendEmptyMessage(111);
                    }

                }.start();
            }
        });

        new Thread(){
            @Override
            public void run() {
                super.run();
                Looper.prepare();
                mHandler = new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        Log.e("sj"," 内部 handle message:"+msg.what);
//                        mBtn.setText("这是内部handle message");
                    }
                };
                Looper.loop();//两个子线程通过handler通信，首先要在该线程内调用Looper.prepare();，创建一个lopper对象,同时构造MessageQueue,手动调用Looper.loop(),否则
//                接受不到消息，下面的打印不会执行,轮询looper内的消息队列。
                Log.e("sj"," 执行结束");

            }
        }.start();
    }





        @Override
    public boolean handleMessage(Message msg) {//当该方法返回false时，void handleMessage依然会执行，并且在该方法执行后执行。如果返回true，void handleMessage
//            不会被执行
            Log.e("sj","callback handle message1");
            mBtn.setText(msg.what+"");
            Log.e("sj","callback handle message2");
        return false;
    }
}
